/**
 *
 * Copyright (C) 2008  Igor Vdovichenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Though a sincere effort has been made to deliver a professional,
 * quality product,the library itself is distributed WITHOUT ANY WARRANTY;
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 */


package com.vin.scriptutils;

import com.vin.scriptutils.utils.UnixCommandBuilder;
import com.vin.scriptutils.utils.ssh.ConnectionInfo;
import com.vin.scriptutils.utils.ssh.ResultReader;
import com.vin.scriptutils.utils.ssh.SshCommand;
import com.vin.scriptutils.utils.ssh.SshTaskExec;
import com.vin.scriptutils.utils.ssh.exception.SshTaskExecException;
import com.vin.scriptutils.utils.ssh.trilead.TrileadSshConnection;
import com.vin.scriptutils.utils.ssh.trilead.TrileadSshExec;
import java.io.IOException;
import java.util.logging.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author igor
 */
public class Sample extends SshCommand{

    private final static Logger logger = LoggerFactory.getLogger(Sample.class);

    Sample(SshTaskExec sshTask) {
        super(sshTask);
    }


    public String mkTmpDir() throws SshTaskExecException, IOException, InterruptedException {
        String tmpName = String.format("tmp_%d", System.currentTimeMillis());
        return mkTmpDir(tmpName);
    }

    public String mkTmpDir(String tmpName) throws SshTaskExecException, IOException, InterruptedException {
        UnixCommandBuilder unixBuilder = new UnixCommandBuilder();
        unixBuilder.command("mkdir").argv("-p").argvQ(tmpName).inSequence().command("pwd");
        ResultReader rd = execute(unixBuilder.toCommandLine());
        if(rd.isError()){
            return null;
        }

        return UString.getUnixRightPathString(rd.getFirstLineStdOutput(), tmpName);
    }

    public void createCloneVmxFile(final String sourceVmx, final String clonedVmx, final String clonedName) throws SshTaskExecException, IOException, InterruptedException {

        UnixCommandBuilder unixBuilder = new UnixCommandBuilder();

        // create remote TMP directory
        String tmpName = String.format("tmp_%d", System.currentTimeMillis());
        String tmpDir = mkTmpDir(tmpName);
        String tmpNameVmx = tmpDir + "/" + tmpName + ".vmx";

        // copy VM-cfg file to TMP directory
        unixBuilder.command("cp")
                .argvQ(sourceVmx)
                .argvQ(tmpNameVmx);
        unixBuilder.toCommandLine();

        execute(unixBuilder.toCommandLine());
        unixBuilder.reset();

        // modify new clone .vmx file
        unixBuilder
                .command("sed").argv("-i")
                .argvEsc(String.format("s/^nvram =.*$/nvram = \"%s.nvram\"/g", clonedName))
                .argvQ(tmpNameVmx)
                .inSequence()
                .command("sed").argv("-i")
                .argvEsc(String.format("s/^extendedConfigFile =.*$/extendedConfigFile = \"%s.vmxf\"/g", clonedName))
                .argvQ(tmpNameVmx)
                .inSequence()
                .command("sed").argv("-i")
                .argvEsc(String.format("s/^displayName =.*$/displayName = \"%s\"/g", clonedName))
                .argvQ(tmpNameVmx)
                .inSequence()
                .command("sed").argv("-i")
                .argvEsc("/^sched.swap.derivedName/d")
                .argvQ(tmpNameVmx)
                .inSequence()
                .command("sed").argv("-i")
                .argvEsc("/^uuid.bios/d")
                .argvQ(tmpNameVmx)
                .inSequence()
                .command("sed").argv("-i")
                .argvEsc("/^uuid.location/d", clonedName)
                .argvQ(tmpNameVmx)
                ;


         execute(unixBuilder.toCommandLine());

        return ;
    }

    public String getMkTmpDirCmd() {
        String tmpName = String.format("tmp_%d", System.currentTimeMillis());
        UnixCommandBuilder unixBuilder = new UnixCommandBuilder();
        unixBuilder.command("mkdir")
                .argv("-p").argvQ(tmpName);
        return unixBuilder.toCommandLine();
    }

    public static void main(String[] args)  {
        try {

            ConnectionInfo ci = new ConnectionInfo()
                .HostAddress("172.16.210.153")
                .Username("root")
                .Password("root2009");

            TrileadSshConnection trileadSsh = (TrileadSshConnection) new TrileadSshConnection(ci)
                .HostAddress("172.16.41.51")
                .Port(22)
                .Username("root")
                .Password("password");
            SshTaskExec sshTask = new TrileadSshExec(trileadSsh);
            Sample sample = new Sample(sshTask);
            String tmpCmnd = sample.mkTmpDir();
            logger.debug(tmpCmnd);
        } catch (SshTaskExecException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
